<template>
  <div class="scheduling_container">
    <el-button
      v-preventReClick
      class="flex_start"
      @click="showDialog(1)"
      type="primary"
      size="small"
      >添加学期</el-button
    >
    <div class="flex_center">
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="title"
        :visible.sync="dialogVisible"
        width="540px"
        :before-close="handleClose"
      >
        <el-form
          :model="formSemester"
          :rules="rules"
          ref="semesterForm"
          label-width="82px"
          class="demo-ruleForm"
        >
          <el-form-item label="学期" prop="semesterName">
            <el-input
          v-input-restrict
              class="semesterName"
              clearable
              v-model="formSemester.semesterName"
              placeholder="请输入学期名称"
            ></el-input>
          </el-form-item>
          <div style="display: flex">
            <el-form-item label="学期时间" prop="beginTime">
              <el-date-picker
                class="date_picker"
                :picker-options="pickerOptionsEnd"
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="formSemester.beginTime"
                type="date"
                clearable
                placeholder="开始日期"
              >
              </el-date-picker>
            </el-form-item>
            <span style="margin: 7px 10px 0">至</span>
            <el-form-item prop="endTime" label-width="0px">
              <el-date-picker
                class="date_picker"
                :picker-options="pickerOptionsStart"
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="formSemester.endTime"
                type="date"
                clearable
                placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </div>
        </el-form>
        <div class="dialog_footer">
          <el-button v-preventReClick @click="handleClose">取 消</el-button>
          <el-button
            v-preventReClick
            type="primary"
            @click="submitForm('semesterForm')"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <div class="scheduling_content" v-if="semesterList.length > 0">
        <div class="content_info" v-loading="loaading">
          <div
            @click="routeTo(item)"
            class="content_info_left"
            v-for="(item, index) in semesterList"
            :key="index"
          >
            <div class="content_info_left_text">{{ item.semesterName }}</div>
            <div class="content_info_left_time">
              {{ item.beginTime | fammterTime }} ~
              {{ item.endTime | fammterTime }}
            </div>
            <div class="btns">
              <i @click.stop="showDialog(2, item)" class="el-icon-edit"></i>
              <img
                @click.stop="delSemester(item.semesterId)"
                class="del_img"
                src="@/assets/images/del_img.png"
                alt=""
              />
            </div>
          </div>
        </div>
      </div>
      <div class="empty_box" v-if="semesterList.length == 0">
        <el-empty description="暂无数据"></el-empty>
      </div>
    </div>
  </div>
</template>

<script>
import {
  addSchoolSemester,
  getSchoolSemesterList,
  updateSchoolSemester,
  deleteSchoolSemester,
} from "@/api/schoolSemester/schoolSemester";

export default {
  name: "courseScheduling",
  data() {
    return {
      loaading: false,
      semesterList: [],
      title: "添加学期",
      dialogVisible: false,
      pickerOptionsStart: {
        // 时间只能大于startTime
        disabledDate: (time) => {
          if (!this.formSemester.beginTime) {
            return;
          }
          return (
            time.getTime() < new Date(this.formSemester.beginTime).getTime()
          );
        },
      },
      pickerOptionsEnd: {
        // 时间只能小于startTime
        disabledDate: (time) => {
          if (!this.formSemester.endTime) {
            return;
          }
          return time.getTime() > new Date(this.formSemester.endTime).getTime();
        },
      },
      formSemester: {
        semesterName: "",
        beginTime: "",
        endTime: "",
      },
      rules: {
        semesterName: [
          { required: true, message: "请输入学期名称", trigger: "blur" },
        ],
        beginTime: [
          { required: true, message: "开始日期不能为空", trigger: "change" },
        ],
        endTime: [
          { required: true, message: "结束日期不能为空", trigger: "change" },
        ],
      },
    };
  },
  filters: {
    fammterTime(val) {
      if (!val) {
        return "";
      }
      var d = new Date(val);
      var year = d.getFullYear();
      var month = d.getMonth() + 1;
      var date = d.getDate();
      var nowTime = `${year}-${month < 10 ? "0" + month : month}-${
        date < 10 ? "0" + date : date
      }`;

      return nowTime;
    },
  },
  activated() {},
  created() {
    this.getSemesterList();
  },
  methods: {
    routeTo(item) {
      this.$router.push({
        path: "/course/addScheduling",
        query: { id: item.semesterId,beginTime:item.beginTime,endTime:item.endTime },
      });
    },
    async getSemesterList() {
      this.loaading = true;
      let res = await getSchoolSemesterList();
      if (res) {
        this.loaading = false;
      } else {
        this.loaading = false;
      }
      this.semesterList = res.rows;
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let params = {
            ...this.formSemester,
            beginTime:this.formSemester.beginTime.slice(0,10) + ' ' + '00:00:00',
            endTime:this.formSemester.endTime.slice(0,10) + ' ' + '23:59:59'
          };
          if (this.title == "添加学期") {
            let res = await addSchoolSemester(params);
            const { code } = res;
            if (code == 200) {
              this.$message.success("添加成功");
              this.$refs["semesterForm"].resetFields();
              this.dialogVisible = false;
              this.getSemesterList();
            }
          } else {
            console.log(params, "params");
            let res = await updateSchoolSemester(params);
            const { code } = res;
            if (code == 200) {
              this.$message.success("修改成功");
              this.$refs["semesterForm"].resetFields();
              this.dialogVisible = false;
              this.getSemesterList();
            }
          }
        } else {
          return false;
        }
      });
    },
    showDialog(type, item) {
      this.title = type == 1 ? "添加学期" : "编辑学期";
      if (type == 2) {
        this.formSemester.semesterId = item.semesterId;
        this.formSemester.semesterName = item.semesterName;
        console.log(
          this.$options.filters["fammterTime"](item.beginTime),
          this.$options.filters["fammterTime"](item.endTime)
        );
        this.formSemester.beginTime = item.beginTime;
        this.formSemester.endTime = item.endTime;
      }
      this.dialogVisible = true;
    },
    handleClose() {
      this.formSemester = {
        semesterName: "",
        beginTime: "",
        endTime: "",
      };
      this.$refs["semesterForm"].resetFields();
      this.dialogVisible = false;
    },
    delSemester(id) {
      this.$confirm("确认要删除该学期吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteSchoolSemester(id);
          const { code } = res;
          if (code == 200) {
            this.$message.success("删除成功！");
            this.getSemesterList();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped lang="scss">
.flex_center {
  display: flex;
  justify-content: center;
}
.flex_clomn_center {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-right: 60px;
  box-sizing: border-box;
}
.scheduling_container {
  width: 100%;
  padding: 15px 20px;
  .scheduling_content {
    width: 1000px;
    .content_info {
      cursor: pointer;
      margin-top: 26px;
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-wrap: wrap;
      &_left {
        margin-bottom: 60px;
        position: relative;
        width: 460px;
        height: 200px;
        border: 2px solid rgba(22, 185, 144, 1);
        border-radius: 6px;
        color: rgba(22, 185, 144, 1);
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        &_text {
          font-size: 30px;
          font-weight: 500;
          color: rgba(22, 185, 144, 1);
        }
        &_time {
          font-size: 20px;
          font-weight: 400;
          color: rgba(22, 185, 144, 1);
          margin-top: 16px;
        }
      }
      &_left:hover {
        .btns {
          display: flex;
        }
      }
    }
  }
}
.btns {
  display: none;
  position: absolute;
  right: 20px;
  top: 20px;
  .el-icon-edit {
    cursor: pointer;
    font-size: 16px;
  }
  .del_img {
    cursor: pointer;
    width: 16px;
    height: 16px;
    object-fit: cover;
    margin-left: 10px;
  }
}
.semesterName {
  width: 314px;
}
.dialog_footer {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.empty_box {
  width: 100%;
  padding: 10vh 0;
  box-sizing: border-box;
  height: 680px;
}
.date_picker {
  width: 140px;
}
</style>
